package cn.edu.gues.infosystam.controller;


import cn.edu.gues.infosystam.entity.*;
import cn.edu.gues.infosystam.service.ReportService;
import cn.edu.gues.infosystam.service.StudentService;
import cn.edu.gues.infosystam.service.TaskService;
import cn.edu.gues.infosystam.service.TemplateService;
import cn.edu.gues.infosystam.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 苏小小
 * @since 2020-06-11
 */
@Controller
@RequestMapping("/Report")
public class ReportController {
    /**
     * 填报
     */
    @Autowired
    private ReportService reportService;

    /**
     * 模板
     */
    @Autowired
    private TemplateService templateService;

    /**
     * 任务
     */
    @Autowired
    private TaskService taskService;

    /**
     * 学生
     */
    @Autowired
    private StudentService studentService;

    /**
     * SESSION
     */
    @Autowired
    private HttpSession httpSession;

    /**
     * 填报列表视图
     * @param model
     * @return
     */
    @RequestMapping("/List")
    public String listReportView(Model model){

        Student student = getUserInfoByStudent();
        if (student == null)
            return "Login";

        IPage<Task> reportTask = reportService.getReportTask(new Page<Task>(1, 10), student.getSno());
        model.addAttribute("reportTask",reportTask);
        return "Report_List";
    }

    /**
     * 信息填报
     * @param templateId 填报模板ID
     * @param model
     * @return
     */
    @GetMapping("Report/{taskId}/{templateId}")
    public String reportView(@PathVariable Integer taskId, @PathVariable Integer templateId, Model model){
        Student student = getUserInfoByStudent();
        if (student == null)
            return "Login";

        Template template = templateService.getById(templateId);
        System.out.println("任务ID： "+taskId);
        System.out.println("模板ID： "+templateId);
        model.addAttribute("taskId", taskId);
        model.addAttribute("template", template);
        return "Report";
    }

    /**
     * 填报详情
     * @param reportId 填报ID
     * @param taskId 任务ID
     * @param model
     * @return
     */
    @GetMapping("Detail/{reportId}/{taskId}")
    public String reportDetailView(@PathVariable String reportId, @PathVariable String taskId, Model model){
        Teacher teacher = getUserInfoByTeacher();
        if (teacher == null)
            return "Login";


        Task task = taskService.getById(taskId);
        Report report = reportService.getById(reportId);
        Template template = templateService.getById(task.getTaskTemplate());
        Student student = studentService.getStudent(report.getSno());

        System.out.println("填报Id："+reportId);
        System.out.println("填报内容："+report.getSno());
        System.out.println("填报内容："+report.getReportContent());
        model.addAttribute("report", report.getReportContent());
        model.addAttribute("template", template);
        model.addAttribute("student", student);
        return "Report_Detail";
    }

    /**
     * 保存填报数据
     * @param report 填报对象
     * @return
     */
    @ResponseBody
    @PostMapping("/Save")
    public R saveReport(Report report){
        Student student = getUserInfoByStudent();
        if (student == null)
            return R.error().message("你尚未登录，请先登录");

        report.setSno(student.getSno());

//        System.out.println("任务ID："+report.getTaskId());
//        System.out.println("模板ID："+report.getSno());
        System.out.println("填报数据："+report.getReportContent());

        boolean save = reportService.save(report);
        if (save)
            return R.success().data("data", report);
        return R.error().message("无法保存填报内容");
    }

    /**
     * 退回填报
     * @param reportId 填报Id
     * @return
     */
    @ResponseBody
    @PostMapping("/Del")
    public R delReport(Integer reportId){
        Teacher teacher = getUserInfoByTeacher();
        if (teacher == null)
            return R.error().message("你尚未登录，请先登录");

        boolean remove = reportService.removeById(reportId);
        if (remove)
            return R.success();
        return R.error();
    }

    /**
     * 获取教师登录信息
     * @return
     */
    private Teacher getUserInfoByTeacher(){
        User userInfo = (User)httpSession.getAttribute("userInfo");
        if (userInfo == null) {
            httpSession.setAttribute("errorMsg", "请先登录");
            // 登录后可以返回的界面
            httpSession.setAttribute("historyHref","/Report/List");
            return null;
        }

        return (Teacher) userInfo.getUserInfo();
    }


    /**
     * 获取登录学生信息
     * @return
     */
    private Student getUserInfoByStudent(){
        User userInfo = (User)httpSession.getAttribute("userInfo");
        if (userInfo == null) {
            httpSession.setAttribute("errorMsg", "请先登录");
            // 登录后可以返回的界面
            httpSession.setAttribute("historyHref","/Report/List");
            return null;
        }

        // 获取登录后的用户信息
        return (Student) userInfo.getUserInfo();
    }

}

